package display2012;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.nio.ByteBuffer;

public class DisplayUDPServer extends Thread {
	
	final int PORT = 4445;
	final int CONNECT = -1;
	
	DatagramSocket serverSocket;
	boolean stop;
		
	public DisplayUDPServer() throws IOException{
		this.serverSocket = new DatagramSocket(PORT);
		this.stop = false;
	}
	
	@Override
	public void run() {
		while(!stop) {
			try {
				byte[] buf = new byte[4];
				DatagramPacket packet = new DatagramPacket(buf, buf.length);
				serverSocket.receive(packet);
				int value = ByteBuffer.allocate(4).put(packet.getData()).getInt();
				if( value == CONNECT)
				{
			    /*
			     * Bisogna implementare qualcosa del tipo
			     * IContaKM addListener(new RemoteProcess(clientSocket));
			     * ma forse bisogna anche cambiare porta...
			     * Come la gestiremo mai?
			     */
				}
			} 
			catch (IOException e) {
			    System.out.println("Accept failed: 4444");
			    System.exit(-1);
			}
		}
	}

}